UNAME := $(shell uname)

ifeq ($(UNAME), Linux)
CCFLAGS = -Iinclude -lpthread -DDEBUG
DEL_CMD = rm
endif
ifeq ($(UNAME), Darwin)
CCFLAGS = -Iinclude -lpthread -DDEBUG -arch x86_64
DEL_CMD = rm
else
CCFLAGS = -Iinclude -lws2_32 -DDEBUG
DEL_CMD = del
endif

EXECUTABLES = ngine_client ngine_server ngine_test sample_client sample_server

COMMON_DIR = src/common/message
COMMON_OBJS = ngmessage.o ngmessagequeue.o ngmessagecoder.o ngdefaultmessagecoder.o
CLIENT_DIR = src/client
CLIENT_OBJS = ngclient.o ngtcpclient.o ngineclient.o
SERVER_DIR = src/server
SERVER_OBJS = nguser.o ngserver.o ngtcpserver.o ngineserver.o

###

all: client server ngine

clean:
	$(DEL_CMD) *.o *.a $(EXECUTABLES)
	
###


common.o: $(COMMON_DIR)/*.cpp
	$(CXX) -c $^ $(CCFLAGS)
	
client.o: $(CLIENT_DIR)/*.cpp
	$(CXX) -c $^ $(CCFLAGS)
	
client: client.o common.o
	$(AR) $(ARFLAGS) libngine_$@.a $(COMMON_OBJS) $(CLIENT_OBJS)
	ranlib libngine_$@.a

server.o: $(SERVER_DIR)/*.cpp
	$(CXX) -c $^ $(CCFLAGS)
	
server: server.o common.o
	$(AR) $(ARFLAGS) libngine_$@.a $(COMMON_OBJS) $(SERVER_OBJS)
	ranlib libngine_$@.a
	
ngine: libngine_server.a libngine_client.a
	$(AR) $(ARFLAGS) lib$@.a $^
	
test_app: client server

sample_chat: client server
	$(MAKE) -C sample\simple_chat

stress_test: client server

### for unit test
GTEST_DIR = test/unit_test/gtest

GTEST_INC = -I$(GTEST_DIR)/include

GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
				$(GTEST_DIR)/include/gtest/internal/*.h

GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)

gtest-all.o: $(GTEST_SRCS_)
	$(CXX) $(GTEST_INC) -I$(GTEST_DIR) -c \
		$(GTEST_DIR)/src/gtest-all.cc
		
gtest_main.o : $(GTEST_SRCS)
	$(CXX) $(GTEST_INC) -I$(GTEST_DIR) -c \
		$(GTEST_DIR)/src/gtest_main.cc
		
gtest.a: gtest-all.o
	$(AR) $(ARFLAGS) $@ $^
	
gtest_main.a : gtest-all.o gtest_main.o
	$(AR) $(ARFLAGS) $@ $^
	ranlib gtest_main.a
		
test: test/unit_test/*.cpp gtest_main.a $(COMMON_OBJS)
	$(CXX) $(GTEST_INC) $^ -o ngine_$@ -lpthread
